home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagn_r.zip / NUMBERS.SWG / 0044_writing bits...pas < prev    next >
Pascal/Delphi Source File  |  1994-05-25  |  923b  |  34 lines

  1. {
  2.  MT>   Could someone please tell me how to write to/read from a particular
  3.  MT>  bit in a number?  Do you have to break the number down into binary
  4.  MT>  or is there some function somewhere that I haven't found yet?
  5.  
  6. Here's some procs I wrote that should help you out:
  7. }
  8.  
  9. Procedure SetBit(Var Number : Byte; Bit : Byte);
  10.  
  11.  Begin
  12.   Number := Number OR (1 SHL Bit);
  13.  End;
  14.  
  15. Procedure ClearBit(Var Number : Byte; Bit : Byte);
  16.  
  17.  Begin
  18.   Number := Number AND NOT (1 SHL Bit);
  19.  End;
  20.  
  21. Function ReadBit(Number, Bit : Byte) : Boolean;
  22.  
  23.  Begin
  24.   ReadBit := (Number AND (1 SHL Bit)) <> 0;
  25.  End;
  26. {
  27. OK, provided you know binary, this should be pretty simple to implement.  The
  28. bits are of course numbered 7-0.  SetBit sets a given bit to 1, ClearBit sets a
  29. given bit to 0, and ReadBit returns TRUE if 1, FALSE if 0.  Anyway, hope that
  30. helps...
  31.  
  32.                                       PsychoMan.
  33. }
  34.